﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace KBEngine.Setting
{
    [Serializable]
    [XmlRoot("root")]
    public class EmailServiceDefaults
    {
        public string smtp_server = string.Empty;
        public string smtp_port = string.Empty;

        /// <summary>
        /// 认证模式 LOGIN=1, PLAIN=2
        /// </summary>
        public int smtp_auth = 2;

        public string username = string.Empty;

        /// <summary>
        /// 密码可使用明文，也可以使用kbe_private.key-RSA加密的密码, 
		/// 填写明文密码LOG会提示警告并给出RSA密码, 简单的从LOG中拷贝过来即可
        /// </summary>
        public string password = string.Empty;

        public Email email_activation = new Email();
        public Email email_resetpassword = new Email();
        public Email email_bind = new Email();

        [Serializable]
        public class Email
        {
            public string subject = string.Empty;

            /// <summary>
            /// 邮件验证有效期
            /// </summary>
            public string deadline = string.Empty;

            [XmlIgnore]
            public string message = string.Empty;
            [XmlIgnore]
            public string backlink_hello_message = string.Empty;
            [XmlIgnore]
            public string backlink_success_message = string.Empty;
            [XmlIgnore]
            public string backlink_fail_message = string.Empty;
          
            [XmlElement("message")]
            public XmlNode[] Node_message
            {
                get
                {
                    var dom = new XmlDocument();
                    return new XmlNode[] { dom.CreateCDataSection(message) };
                }
                set
                {
                    if (value == null)
                    {
                        message = null;
                        return;
                    }

                    if (value.Length != 1)
                        throw new InvalidOperationException("Invalid array.");
                    var content = value[0];
                    message = null == content ? string.Empty : content.Value;
                }
            }

            [XmlElement("backlink_hello_message")]
            public XmlNode[] Node_backlink_hello
            {
                get
                {
                    var dom = new XmlDocument();
                    return new XmlNode[] { dom.CreateCDataSection(backlink_hello_message) };
                }
                set
                {
                    if (value == null)
                    {
                        backlink_hello_message = null;
                        return;
                    }

                    if (value.Length != 1)
                        throw new InvalidOperationException("Invalid array.");
                    var content = value[0];
                    backlink_hello_message = null == content ? string.Empty : content.Value;
                }
            }

            [XmlElement("backlink_success_message")]
            public XmlNode[] Node_backlink_success
            {
                get
                {
                    var dom = new XmlDocument();
                    return new XmlNode[] { dom.CreateCDataSection(backlink_success_message) };
                }
                set
                {
                    if (value == null)
                    {
                        backlink_success_message = null;
                        return;
                    }

                    if (value.Length != 1)
                        throw new InvalidOperationException("Invalid array.");
                    var content = value[0];
                    backlink_success_message = null == content ? string.Empty : content.Value;
                }
            }

            [XmlElement("backlink_fail_message")]
            public XmlNode[] Node_backlink_fail
            {
                get
                {
                    var dom = new XmlDocument();
                    return new XmlNode[] { dom.CreateCDataSection(backlink_fail_message) };
                }
                set
                {
                    if (value == null)
                    {
                        backlink_fail_message = null;
                        return;
                    }

                    if (value.Length != 1)
                        throw new InvalidOperationException("Invalid array.");
                    var content = value[0];
                    backlink_fail_message = null == content ? string.Empty : content.Value;
                }
            }
        }
    }
}
